package com.recipedb.sprint.repository;

import com.recipedb.sprint.entity.Sessions;
import io.lettuce.core.dynamic.annotation.Param;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
public interface SessionsRepository extends JpaRepository<Sessions, Long> {
    List<Sessions> findByUseridOrderByCreatedAtDesc(Long userid);
    List<Sessions> findAllByOrderByCreatedAtDesc();
    List<Sessions> findByUserid(Long userId);

    @Modifying
        @Transactional
        @Query("UPDATE Sessions s SET s.answer = :answer WHERE s.sessionid = :sessionId")
        void updateAnswerBySessionid(@Param("sessionId") Long sessionId, @Param("answer") String answer);

        @Query("select s from Sessions s where s.answer is not null or s.prompt is not null")
        List<Sessions> findAllWithAnswerOrPrompt();
    }
